#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "stringTools.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "joueur.h"

int sendName(int socket ,char * name){
	
	
	char connectCommand[30];
    
    memset(connectCommand,0,sizeof(connectCommand));
    
	//Generation de la commande CONNECT
	
	strcat(connectCommand,"CONNECT/");
	strcat(connectCommand,name);
	strcat(connectCommand,"/\n");
    
	// Envoyer le message de Connection au serveur
	if (send(socket,connectCommand,sizeof(connectCommand),0) ==-1){
		perror("write");
		exit(1);
	}
	return 0;
}


void drawPlan( joueur *player  ){
	int i,j;
	
	printf("      A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  \n");		

	
	for(i=0;i<16;i++){
		if(i<10){
			printf("%d  | ",i);
		}
		else{
				printf("%d | ",i);

		}
		for(j=0;j<16;j++){
			if(player->positions[i][j] == 0){
				printf(" . ");
			}
			if(player->positions[i][j] == 1){
				printf(" 0 ");
			}
			if(player->positions[i][j] == 2){
				printf(" @ ");
			}
			if(player->positions[i][j] == -1){
				printf(" * ");
			}
			if(player->positions[i][j] == -2){
				printf(" X ");
			}
		}
		printf(" |\n");
	}
	
	
}



void putMySub(int s ,joueur * player) {

    char confirmationResponse[15];
	char connectCommand[30];
	char connectResponse[30];
	
	int i,ord=0;
	
    int ship=0;
    int x=0,y=0,cmpX=0,cmpY=0;
	int counterShip=0;
    while(1){
		printf("nouveau tour :\n");
        memset(connectCommand,0,sizeof(connectCommand));
        memset(confirmationResponse,0,sizeof(confirmationResponse));
        memset(connectResponse,0,sizeof(connectResponse));
        
        if( recv(s,connectResponse,sizeof(connectResponse),0) ==-1) {
            perror ("recvfrom");
            exit(1);
        }
        
        printf("%s\n",connectResponse);
        
        scanf("%s",connectCommand);
		
        strcat(connectCommand, "\n");
		printf("******  %s\n",connectCommand);
        if (send(s,connectCommand,sizeof(connectCommand),0) ==-1){
            perror("write");
            exit(1);
        }
        
        if( recv(s,confirmationResponse,sizeof(confirmationResponse),0) ==-1) {
            perror ("recvfrom");
            exit(1);
        }
        
        printf("%s",confirmationResponse);
        printf("\n");
        if(strcmp(confirmationResponse, "OK/\n")==0){
			printf("if du OK OK\n");
            char** mot3=splitString(connectCommand, '/');
            int nbcoord=(countChars(connectCommand, '/')-1);
            for(i=1;i<=nbcoord;i++){
                if(i%2!=0){
                    x=atoi(mot3[i]);
                    printf(" x= %d\n",x);
                }
                else{
                    ord=(mot3[i][0]);
                    y= ord -'A';
                    player->subs[ship]->xy[cmpX]=createCoordonne(x,y);
                    printf(" y= %d\n",y);
                    player->positions[x][y]=-1;
                    cmpX++;
                }
            }
            ship++; 
			counterShip++;
			drawPlan(player  );
        }
        
        
        if(strcmp(confirmationResponse, "ALLYOURBASE/")==0 || counterShip==2){
            break;
        }
         
    }
}
